
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Custom error constructor factory.
tea-error
is available on npm.
$ npm install tea-error
tea-error
is available as a component.
$ component install qualiancy/tea-error
The primary export for this module is a factory that will return a constructor for a custom error of the type/name that is specified.
var error = require('tea-error')
, TestError = error('TestError')
, err = new TestError('My error message.');
throw err;
An error created/thrown from the custom constructor
observes all of the expected Javascript behaviors of
Error
.
The constructed error is an instanceof an Error.
var err = new Error();
, myerr = new TestError()
err.should.be.instanceof(Error);
myerr.should.be.instanceof(Error);
myerr.should.be.isntanceof(TestError);
The name
property is defined like other "native" errors.
var err = new ReferenceError()
, myerr = new TestError();
err.should.have.property('name', 'ReferenceError');
myerr.should.have.property('name', 'TestError');
The message
property is defined like all error and specified
as the first argument during construction.
var err = new Error('Javascript error occurred')
, myerr = new TestError('Test error occurred');
err.should.have.property('message', 'Javascript error occurred');
myerr.should.have.property('message', 'Test Error occurred');
If the environment provides the ability to capture a stack trace,
it will be provided as the stack
property. Please view your environments
documentation to see if this is supported.
var err = new Error()
, myerr = new TestError();
if (Error.captureStackTrace) {
err.should.have.property('stack');
myerr.should.have.property('stack');
}
Once the new constructor for an error has been created it can be used to construct errors just as normal javascript errors.
var TestError = require('tea-errors')('TestError')
, err = new TestError('that did not work right');
throw err;
There are a number of additional arguments that can be specified upon construction to provide further insight into the error created.
The second argument during construction can be an object of properties that will be merged onto the newly created error.
var err = new TestError('expected field value', { fields: [ 'username' ] });
err.should.have.property('fields')
.an('array')
.that.deep.equals([ 'username' ]);
Note that if the name
, message
, and stack
properties are defined
in this custom properties object, they will be ignored as those keys
are reserved.
The start stack function is used by Error.captureStackTrace
to indicate where the environment should start the visible
stack trace. Modifying this value might be useful in situations
where the internals of the module issuing the error are irrelevant
to what occured. The most obvious example is providing feedback
for an api method.
// api.js
var ApiError = require('tea-error')('ApiError');
exports.use = function (fn) {
if ('function' !== typeof fn) {
throw new ApiError('API .use only accepts functions', null, arguments.callee);
}
// etc.
};
// user.js
var api = require('./api.js);
api.use('a string?');
In this scenario the first line of the error stack will reference
user.js-Ln:2
as opposed to api.js-Ln:5
. See the stack
example
for a demonstration.
Convert this error into a serialized JSON object.
var err = new TestError('some message', { hello: 'universe' })
, json = err.toJSON();
json.should.deep.equal({
name: 'TestError'
, message: 'some message'
, hello: 'universe'
, stack: '...'
});
If this method is called as err.toJSON(false)
the
stack
property will not be included.
(The MIT License)
Copyright (c) 2012 Jake Luer jake@qualiancy.com (http://qualiancy.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Custom error constructor factory.
We found that tea-error demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.